Java: Using Assertion in Eclipse

Categories: Java; Tagged with: ; @ May 21st, 2012 0:06

Assertion:

	public static void main(String[] args) {
		int x = 10;
		assert x==100:"assertion failed!";
	}

How to determine assertion is enabled or not?

		   boolean isEnable=false;
		   assert isEnable = true;
		   if(isEnable){
		       throw new RuntimeException("Assertion shoule be enable!");
		   }

This is a bad assertion, because it change the value, If assertion is enabled, isEnable will be set to true.

How to enable assertion in Eclipse?

Debug/Run configurations > VM arguments: add “-ea” to enable it or “-da” to disable assertion.

should not used for validate parameter in public method: because anyone can use public method, and can not make sure assertion is enabled or not;

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.